/*
 * Project: Sudoku Explainer
 * Copyright (C) 2006-2007 Nicolas Juillerat
 * 
 * Project: Sudoku Hint Engine for Android
 * Copyright (C) 2010-2013 Emanuel Moecklin
 * 
 * Available under the terms of the Lesser General Public License (LGPL)
 */
package com.onegravity.sudoku.solver.rules.uniqueness;

import java.util.BitSet;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.onegravity.sudoku.solver.HintResource;
import com.onegravity.sudoku.solver.HintType;
import com.onegravity.sudoku.solver.data.Cell;
import com.onegravity.sudoku.solver.data.ImmutableCell;
import com.onegravity.sudoku.solver.data.Region;
import com.onegravity.sudoku.solver.util.HtmlLoader;

public class UniquenessTestType4Hint extends UniquenessTestHint {

	private static final long serialVersionUID = 8598736159523590693L;
	
	private final Cell c1;
    private final Cell c2;
    private final int lockValue;
    private final int remValue;
    private final Region region;

    public UniquenessTestType4Hint(List<Cell> loop, int lockValue,
            int remValue, Map<Cell, BitSet> removablePotentials, Cell c1, Cell c2,
            Region region) {
        super( loop, lockValue, remValue, removablePotentials);
        this.c1 = c1;
        this.c2 = c2;
        this.lockValue = lockValue;
        this.remValue = remValue;
        this.region = region;
    }

    @Override
    public Collection<Cell> getSelectedCells(HintType hintType) {
    	switch(hintType) {
    	case SMALL_CLUE: return super.getSelectedCells(hintType);
    	case HINT: 
    		// doesn't look good
    		// return Arrays.asList(new Cell[] {c1, c2}); 
    	case BIG_CLUE:
    	default: return null;
    	}
    }

    @Override
    public Map<Cell, BitSet> getSelectedPotentials(HintType hintType, int viewNum) {
        return (hintType!=HintType.SMALL_CLUE) ? super.getSelectedPotentials(hintType, viewNum) : null;
    }

    @Override
    public Map<Cell, BitSet> getSecondarySelectedPotentials(HintType hintType, int viewNum) {
        Map<Cell, BitSet> result = new HashMap<Cell, BitSet>();
        BitSet p1 = (BitSet) c1.getPotentialValues().clone();
        p1.clear(lockValue);
        p1.clear(remValue);
        BitSet p2 = (BitSet) c2.getPotentialValues().clone();
        p2.clear(lockValue);
        p2.clear(remValue);
       	result.put(c1, p1);
       	result.put(c2, p2);
        return (hintType==HintType.HINT) ? result : null;
    }

    @Override
    public Map<ImmutableCell, BitSet> getRemovedPotentials(HintType hintType) {
        return (hintType==HintType.HINT) ? super.getRemovedPotentials(hintType) : null;
    }

    @Override
    public Region[] getRegions(HintType hintType) {
        return (hintType==HintType.HINT) ? new Region[] {region} : null;
    }

    @Override
    protected int getType() {
        return 4;
    }
    
    @Override
    protected HintResource getResource() {
        return HintResource.UNIQUENESS_TEST_4;
    }

    @Override
    public String getHintHtml(HintType hintType) {
        String result = HtmlLoader.loadHtml(HintResource.UNIQUENESS_TEST_4.hint(hintType));
        String type = getTypeName();
        Cell[] cells = new Cell[loop.size()];
        loop.toArray(cells);
        String allCells = Cell.toString(cells);
        String cell1 = c1.toString();
        String cell2 = c2.toString();
        result = HtmlLoader.format(result, type, lockValue, remValue, allCells, cell1, cell2, region.toFullString(), loop.size());
        return result;
    }

}